import sys, file_ops
from riiv import get_riiv_patches_inf
from riiv import apply_riiv_patches_wrapper

# remove if they are there:
file_ops.rm_folder("tmp")
file_ops.rm_file("tmp_wit.xml")
file_ops.rm_file("tmp_gl.txt")
file_ops.rm_file("result.wbfs")

sections = get_riiv_patches_inf(sys.argv[1], sys.argv[2], sys.argv[3])

if (sections == None):
  print("Riivolution XML is faulty")
  exit(1)

print()
# possible choices
pos_choices = []
for sec in sections:
  print("Section name: %s" % (sec.name))
  print("#-Options names:")
  for opt in sec.options:
    print("----%s" % (opt.name))
    print("##----Choices names:")
    for cho in opt.choices:
      print("--------%s" % (cho.name))
      pos_choices.append(cho.name)
      print("###-------Patches ids:")
      for pat in cho.patches:
        print("------------%s" % (pat.id))
print()

print("Please, select the \"Choices\" to apply to the game.")
print("Example: " + sections[0].options[0].choices[0].name + " ...")
input_choices = input("Enter: ")
input_choices = input_choices.split()

for cho in input_choices:
  if (cho not in pos_choices):
    print("\nInvalid choice: " + cho)
    exit(1)

print("\nSelected choices: ", end = "")
print(input_choices)

result = apply_riiv_patches_wrapper(sys.argv[1], sys.argv[2], sys.argv[3], input_choices)
if (result):
  print("Done!")
else:
  print("Errors happened...")
  exit(1)
exit(0)
